

package com.maoshi.shop.multishop.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.maoshi.shop.bean.enums.AuditStatus;
import com.maoshi.shop.bean.enums.SmsType;
import com.maoshi.shop.bean.model.ShopBankCard;
import com.maoshi.shop.bean.model.ShopDetail;
import com.maoshi.shop.bean.model.ShopWithdrawCash;
import com.maoshi.shop.common.config.Constant;
import com.maoshi.shop.common.exception.MaoshiShopBindException;
import com.maoshi.shop.security.multishop.util.SecurityUtils;
import com.maoshi.shop.service.ShopBankCardService;
import com.maoshi.shop.service.ShopDetailService;
import com.maoshi.shop.service.ShopWithdrawCashService;
import com.maoshi.shop.service.SmsLogService;
import lombok.AllArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Objects;


/**
 * 商家提现申请信息
 *
 * @author Dwl
 * @date 2019-09-19 14:22:08
 */
@RestController
@AllArgsConstructor
@RequestMapping("/shop/withdrawCash" )
public class ShopWithdrawCashController {

    private final ShopWithdrawCashService shopWithdrawCashService;
    private ShopDetailService shopDetailService;
    private SmsLogService smsLogService;


    @PostMapping("/apply")
    public ResponseEntity<Void> apply(@RequestBody ShopWithdrawCash shopWithdrawCash) {
        ShopDetail shopDetail = shopDetailService.getById(SecurityUtils.getShopUser().getShopId());
        if(Objects.equals(shopDetail.getShopId(), Constant.MAIN_SHOP)){
            throw  new MaoshiShopBindException("自营店无法提现");
        }
        if(shopWithdrawCash.getAmount() < 1){
            throw new MaoshiShopBindException("提现金额不能少于1.00元");
        }
        if (!smsLogService.checkValidCode(shopDetail.getMobile(), shopWithdrawCash.getCode(), SmsType.VALID)){
            throw new MaoshiShopBindException("验证码有误或已过期");
        }

        shopWithdrawCash.setUpdateTime(new Date());
        shopWithdrawCash.setCreateTime(new Date());
        shopWithdrawCash.setStatus(AuditStatus.WAITAUDIT.value());
        shopWithdrawCash.setShopId(SecurityUtils.getShopUser().getShopId());

        shopWithdrawCashService.withdrawCash(shopWithdrawCash);
        return ResponseEntity.ok().build();
    }

    @GetMapping("/getShopInfo")
    public ResponseEntity<ShopWithdrawCash> getShopInfo() {
        Long shopId = SecurityUtils.getShopUser().getShopId();
        ShopWithdrawCash shopWithdrawCash = shopWithdrawCashService.getShopWithdrawCashByShopId(shopId);
        return ResponseEntity.ok(shopWithdrawCash);
    }
}
